package com.youtui.manager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.youtui.common.constant.ResultMessage;
import com.youtui.common.util.DateUtils;
import com.youtui.common.util.Md5Util;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.model.Developer;
import com.youtui.manager.service.IDeveloperService;

@Controller
@RequestMapping("/developer")
public class DeveloperController extends BaseController {

	@Autowired
	IDeveloperService developerService;

	@RequestMapping("/pagelist.do")
	@ResponseBody
	public ResultMessage list(Developer developer, @RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "20") Integer rows) {
		PageInfo<Developer> pageinfo = developerService.findDeveloperPagelist(developer, page, rows);
		return ResultUtil.result(ResultUtil.code_success, "开发者列表", pageinfo);
	}

	@RequestMapping("/load.do")
	@ResponseBody
	public ResultMessage list(Long id) {

		if (id == null) {
			return ResultUtil.result(ResultUtil.code_fail, "开发者详情获取失败,参数错误");
		}

		Developer developer = developerService.findDeveloperById(id);
		if (developer != null) {
			return ResultUtil.result(ResultUtil.code_success, "开发者详情", developer);
		}
		return ResultUtil.result(ResultUtil.code_fail, "开发者详情获取失败");
	}

	@RequestMapping("/update.do")
	@ResponseBody
	public ResultMessage update(Developer developer) {

		if (developer == null || developer.getId() == null) {
			return ResultUtil.result(ResultUtil.code_fail, "参数错误");
		}

		developerService.update(developer);
		
		return ResultUtil.result(ResultUtil.code_fail, "开发者信息更新成功");
	}

	@RequestMapping("/update_pwd.do")
	@ResponseBody
	public ResultMessage updatePwd(Long id, String oldPwd, String newPwd) {

		if (id == null || StringUtils.isBlank(newPwd) || StringUtils.isBlank(oldPwd)) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}
		if (oldPwd.equals(newPwd)) {
			return ResultUtil.result(ResultUtil.code_fail, "修改前后密码不能一致");
		}

		Developer developer = developerService.findDeveloperById(id);
		if (developer == null) {
			return ResultUtil.result(ResultUtil.code_fail, "开发者信息不存在");
		}

		if (!developer.getPassword().equals(Md5Util.md5(oldPwd))) {
			return ResultUtil.result(ResultUtil.code_fail, "原始密码错误");
		}

		developer.setPassword(Md5Util.md5(newPwd));
		developerService.update(developer);

		return ResultUtil.result(ResultUtil.code_success, "密码修改成功");
	}

	@RequestMapping("/reset_pwd.do")
	@ResponseBody
	public ResultMessage resetPwd(Long id) {
		Developer developer = developerService.findDeveloperById(id);
		if (developer == null) {
			return ResultUtil.result(ResultUtil.code_fail, "开发者信息不存在");
		}
		developer.setPassword(Md5Util.md5("666666"));
		developerService.update(developer);
		return ResultUtil.result(ResultUtil.code_success, "密码重置成功，新密码为：666666");
	}
	

	@RequestMapping("/export.do")
	@ResponseBody
	public void export(Developer developer) throws Exception {
		List<Developer> reportList = developerService.findDeveloperlist(developer);
		 
		String fileName = "开发者信息报表" + DateUtils.getTimestamp();
		Map<String, Object> dataMap = new HashMap<String, Object>();
 
		excelUtil.excelData(request, response, fileName, "developer_excel", reportList, dataMap);
	}

}
